package com.jzx.provider.ucenter;

import com.jzx.provider.common.constants.Expire;
import com.jzx.provider.common.utils.JwtUtil;
import com.jzx.provider.exception.MyErrorType;
import com.jzx.provider.exception.UcenterRuntimeException;
import com.jzx.provider.ucenter.client.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author daihuihui
 * @date 2019/2/7
 */
@Service
public class UcenterService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 退出登录
     */
    public void logout(String token) {
        stringRedisTemplate.delete(token);
    }

    /**
     * 刷新token过期时间
     */
    public void renewToken(String token, String platform) {

        stringRedisTemplate.expire(token, Expire.USER, TimeUnit.MINUTES);
    }

    /**
     * 判断token是否过期
     *
     * @platform consul user hr {com.jzx.provider.common.constants.PlatForm}
     */
    public void isLogin(String token, String platform) {
        Map<String, String> map = null;
        try {
            map = JwtUtil.verifyToken(token);
            if (StringUtils.isEmpty(map.get("id"))) {
                throw new UcenterRuntimeException(MyErrorType.USER_NOT_LOGIN);
            }
            String key = platform + "_" + map.get("id").toString() + "_login";
            if (!stringRedisTemplate.hasKey(key)) {
                throw new UcenterRuntimeException(MyErrorType.USER_NOT_LOGIN);
            }
            String[] mes =stringRedisTemplate.opsForValue().get(key).split(",");
            if (!token.equals(mes[0])) {
                throw new UcenterRuntimeException(MyErrorType.USER_LOGIN_OTHER);
            }
            //刷新过期时间
            stringRedisTemplate.expire(key, Expire.USER, TimeUnit.MINUTES);
        } catch (Exception e) {
            throw new UcenterRuntimeException(MyErrorType.USER_NOT_LOGIN);
        }
    }

    /**
     * 设置key的value值
     *
     * @param key
     * @param value
     */
    public void setValueOfKey(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    /**
     * 获取key的value值
     */
    public String getValueOfKey(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 获取当前用户的部分信息
     */
    public CurrentUser findUserBytoken(String token, String platform) {
        Map<String, String> map = null;
        try {
            map = JwtUtil.verifyToken(token);
        } catch (Exception e) {
            throw new UcenterRuntimeException(MyErrorType.USER_NOT_LOGIN);
        }
        String key = platform + "_" + map.get("id").toString() + "_login";
        String[] mes =stringRedisTemplate.opsForValue().get(key).split(",");
        CurrentUser currentUser = CurrentUser.builder()
                .id(Integer.valueOf(map.get("id")))
                .mobile(mes[1]).type(platform).build();
        return currentUser;
    }
}
